var http = require('http');
var fs = require('fs');
var cheerio = require('cheerio');
var request = require('request');
const Entities = require('html-entities').XmlEntities;
var url = "http://gdjy.hfut.edu.cn"; 

var db = require('./sqldb/index.js');
var job =require('./model/job.js')
const entities = new Entities();
//初始url 

function getPage(){
	return new Promise((resolve,reject)=>{
		request({url:`${url}/products/list/1.html?list=a`,encoding: null}, (error, response, body) => {
			if (!error && response.statusCode == 200) {
				const $ = cheerio.load(body);
				let page=Math.ceil($('#w0 .summary b').eq(1).html()/18)
				resolve(page)
				// console.log(entities.decode($('#w0 table tr').eq(1).children().eq(0).find('a').html()))
			}else {
				console.log(error)
			}
		});
	})
}

function insert(data){
	return db.sequelize.transaction(function(t){
		console.log("+++++++++++++++++++")
		return job(db.sequelize).create(data,{
			transaction:t
		}).then(function(data){
		}).catch(function(err){
			console.log("发生错误：" + err);
		});
	})
}


// getPage()
// .then((page)=>{
// 	for (var i = 1; i <= page; i++) {
// 		(function(i){
// 			request({url:url+'/products/list/1.html?list=a&page='+i+'&per-page=18',encoding: null}, (error, response, body) => {
// 				if (!error && response.statusCode == 200) {
// 					const $ = cheerio.load(body);
// 					let long=0

// 					long = $('#w0 .summary b').eq(0).html().split('-')[1] - $('#w0 .summary b').eq(0).html().split('-')[0] + 1;
// 					console.log(`页数：${i}，条数：${long}`)
// 					for (var j = 1; j <= long; j++) {
// 						(function(j){
// 							let data={
// 								id: $('#w0 table tr').eq(j).attr('data-key'),
// 								click: $('#w0 table tr').eq(j).children().eq(3).html(),
// 								company: entities.decode($('#w0 table tr').eq(j).children().eq(0).find('a').html()),
// 								place: entities.decode($('#w0 table tr').eq(j).children().eq(2).html()),
// 								time: $('#w0 table tr').eq(j).children().eq(1).html()
// 							}
// 							insert(data)
// 						})(j)
// 					}

// 				}else {
// 					console.log(error)
// 				}
// 			});
// 		})(i)
		
// 	}
// })


function getId(){
	return new Promise((resolve,reject) => {
		db.sequelize.transaction(function(t){
			console.log("+++++++++++++++++++")
			return job(db.sequelize).findAll({
				attributes: ['id']
			},{
				transaction:t
			}).then(function(data){
				resolve(data)
			}).catch(function(err){
				console.log("发生错误：" + err);
			});
		})
	})
}

function updateInfo(id,data){
	return db.sequelize.transaction(function(t){
		console.log("+++++++++++++++++++")
		return job(db.sequelize).update(data,{
			where:{
				id:id
			}
		},{
			transaction:t
		}).then(function(data){
		}).catch(function(err){
			console.log("发生错误：" + err);
		});
	})
}


getId()
.then((data) => {
	for (var i = 0; i < data.length; i++) {
		(function(i){
			// console.log(data[i].id)
			request({url:`http://gdjy.hfut.edu.cn/products/${data[i].id}.html`}, (error, response, body) => {
				if (!error && response.statusCode == 200) {
					const $ = cheerio.load(body);
					let info={
						info:entities.decode($(".site-index .body-content .row .col-lg-9 .panel-body").eq(0).html())
					}
					updateInfo(data[i].id,info)
				}else {
					console.log(error)
				}
			});
		})(i)
	}
})

